#!/bin/bash

# 流量实时查看脚本
while [ True ];do
    i=0
    for eth in $(cat /proc/net/dev | sed -e '1,2d'|cut -d: -f1);do

        RX[$i]=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
        TX[$i]=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
        let i=$i+1
    done

    sleep 1
    clear
    i=0
    for eth in $(cat /proc/net/dev | sed -e '1,2d'|cut -d: -f1);do
        RXnext[$i]=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
        TXnext[$i]=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
        let i=$i+1
    done

    i=0
    echo  -e  "\t RX `date +%k:%M:%S` TX"    
    for eth in $(cat /proc/net/dev | sed -e '1,2d'|cut -d: -f1);do
        RX=$((${RXnext[$i]}-${RX[$i]}))
        TX=$((${TXnext[$i]}-${TX[$i]}))
         
        if [[ $RX -lt 1024 ]];then
        RX="${RX}B/s"
        elif [[ $RX -gt 1048576 ]];then
        RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
        else
        RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
        fi
         
        if [[ $TX -lt 1024 ]];then
        TX="${TX}B/s"
        elif [[ $TX -gt 1048576 ]];then
        TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
        else
        TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
        fi
         
        echo -e "$eth \t $RX   $TX "
        let i=$i+1
    done
done
